using UnityEngine;
using System.Collections;

public class ConstruirLabirinto : MonoBehaviour {
	public string nomeImagemDoLabirinto;
	private Texture2D labirinto;
	public GameObject[] listObjectPrefab;
	public Color[] cores;
	
	void Start() {
		labirinto = Resources.Load(nomeImagemDoLabirinto) as Texture2D;
		if(labirinto != null && 
		   listObjectPrefab != null &&
			cores != null &&
			cores.Length == listObjectPrefab.Length) {
			// Varre todo o labirinto e cria os objetos 
			float posXInicial = transform.position.x;
			float posYInicial = transform.position.y;
			for(float x = posXInicial; x < posXInicial + labirinto.width; x++) {
				for(float y = posYInicial; y < posYInicial +labirinto.height; y++) {
					InstanciarObjeto(labirinto.GetPixel((int)(x-posXInicial),(int)(y-posYInicial)),x,y);
				}
			}
			
		}
	}
	
	void InstanciarObjeto(Color cor,float x, float y) {
		int indiceCor = 0;
		
		for(int i = 0; i < cores.Length; i++) {
			//Debug.Log("cores[i] = " + cores[i]);
			if(cores[i].r == cor.r && cores[i].g == cor.g && cores[i].b == cor.b) {
				indiceCor = i;
			}
		}
		//Debug.Log(cor);
		GameObject objeto = (GameObject)(Instantiate(listObjectPrefab[indiceCor]));
		objeto.transform.position = new Vector3(x,0,y);
		objeto.transform.Rotate(new Vector3(x*10,y*10,y*10));
		objeto.transform.parent = gameObject.transform;
	} 
	
}
